package com.grades.springboot.service.impl;

import com.grades.springboot.entity.Result;
import com.grades.springboot.entity.User;
import com.grades.springboot.mapper.AdminMapper;
import com.grades.springboot.mapper.TeacherMapper;
import com.grades.springboot.service.LoginService;
import com.grades.springboot.util.Encrypt;
import com.grades.springboot.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private ValidationServiceImpl validationService;
    @Autowired
    private CaptchaServiceImpl captchaService;

    @Override
    public Result<?> loginById(Integer id, String password, String code,String userType, HttpServletRequest request) {
        if (!captchaService.checkCaptcha(code,request)){
            return Result.error("验证码错误");
        }
        User user;
        if (userType.equals("admin")){
            user = adminMapper.selectById(id);
        } else if (userType.equals("teacher")) {
            user = teacherMapper.selectById(id);
        }else{
            return Result.error("不存在的用户类型");
        }

        if (user == null){
            return Result.error("此id不存在，请查证后重新输入！");
        }else if (!user.getPassword().equals(Encrypt.toMD5(password))){
            return Result.error("密码错误");
        }
        return Result.success(newToken(id,userType,user.getName()));
    }

    @Override
    public Result<?> loginByEmail(String email, String code,String userType) {
        User user;
        if (userType.equals("admin")){
            user = adminMapper.selectByEmail(email);
        } else if (userType.equals("teacher")) {
            user = teacherMapper.selectByEmail(email);
        }else{
            return Result.error("不存在的用户类型");
        }

        if (user == null){
            return Result.error("此邮箱还未注册，请查证后重新输入！");
        }else if (!validationService.checkValidation(email,Encrypt.toMD5(code))){
            return Result.error("验证码错误或已过期");
        }else{
            return Result.success(newToken(user.getId(),userType,user.getName()));
        }
    }

    @Override
    public String newToken(Integer id,String uerType, String name) {
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",id);
        claims.put("userType",uerType);
        claims.put("name",name);
        return JwtUtil.genToken(claims);
    }

//    @Override
//    public String newToken(String email) {
//        User user = userMapper.findUserByEmail(email);
//        Map<String,Object> claims = new HashMap<>();
//        claims.put("id",user.getId());
//        claims.put("name",user.getName());
//        claims.put("email",user.getEmail());
//        return JwtUtil.genToken(claims);
//    }
}
